package cn.iocoder.yudao.module.dr.controller.admin.collectionlinkaddress;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.dr.controller.admin.collectionlinkaddress.vo.CollectionLinkAddressPageReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.collectionlinkaddress.vo.CollectionLinkAddressRespVO;
import cn.iocoder.yudao.module.dr.controller.admin.collectionlinkaddress.vo.CollectionLinkAddressSaveReqVO;
import cn.iocoder.yudao.module.dr.dal.dataobject.collectionlinkaddress.CollectionLinkAddressDO;
import cn.iocoder.yudao.module.dr.service.collectionlinkaddress.CollectionLinkAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 藏品模型链接地址")
@RestController
@RequestMapping("/dr/collection-link-address")
@Validated
public class CollectionLinkAddressController {

    @Resource
    private CollectionLinkAddressService collectionLinkAddressService;

    @PostMapping("/create")
    @Operation(summary = "创建藏品模型链接地址")
    //@PreAuthorize("@ss.hasPermission('dr:collection-link-address:create')")
    public CommonResult<Long> createCollectionLinkAddress(@Valid @RequestBody CollectionLinkAddressSaveReqVO createReqVO) {
        return success(collectionLinkAddressService.createCollectionLinkAddress(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新藏品模型链接地址")
    //@PreAuthorize("@ss.hasPermission('dr:collection-link-address:update')")
    public CommonResult<Boolean> updateCollectionLinkAddress(@Valid @RequestBody CollectionLinkAddressSaveReqVO updateReqVO) {
        collectionLinkAddressService.updateCollectionLinkAddress(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除藏品模型链接地址")
    @Parameter(name = "id", description = "编号", required = true)
   // @PreAuthorize("@ss.hasPermission('dr:collection-link-address:delete')")
    public CommonResult<Boolean> deleteCollectionLinkAddress(@RequestParam("id") Long id) {
        collectionLinkAddressService.deleteCollectionLinkAddress(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得藏品模型链接地址")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('dr:collection-link-address:query')")
    public CommonResult<CollectionLinkAddressRespVO> getCollectionLinkAddress(@RequestParam("id") Long id) {
        CollectionLinkAddressDO collectionLinkAddress = collectionLinkAddressService.getCollectionLinkAddress(id);
        return success(BeanUtils.toBean(collectionLinkAddress, CollectionLinkAddressRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得藏品模型链接地址分页")
    //@PreAuthorize("@ss.hasPermission('dr:collection-link-address:query')")
    public CommonResult<PageResult<CollectionLinkAddressRespVO>> getCollectionLinkAddressPage(@Valid CollectionLinkAddressPageReqVO pageReqVO) {
        PageResult<CollectionLinkAddressDO> pageResult = collectionLinkAddressService.getCollectionLinkAddressPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CollectionLinkAddressRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得藏品模型链接地址列表")
    //@PreAuthorize("@ss.hasPermission('dr:collection-link-address:query')")
    public CommonResult<List<CollectionLinkAddressRespVO>> getCollectionLinkAddressList(@Valid CollectionLinkAddressPageReqVO pageReqVO) {
        List<CollectionLinkAddressDO> listResult = collectionLinkAddressService.getCollectionLinkAddressList(pageReqVO);
        return success(BeanUtils.toBean(listResult, CollectionLinkAddressRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出藏品模型链接地址 Excel")
    @PreAuthorize("@ss.hasPermission('dr:collection-link-address:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCollectionLinkAddressExcel(@Valid CollectionLinkAddressPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CollectionLinkAddressDO> list = collectionLinkAddressService.getCollectionLinkAddressPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "藏品模型链接地址.xls", "数据", CollectionLinkAddressRespVO.class,
                        BeanUtils.toBean(list, CollectionLinkAddressRespVO.class));
    }

}